package com.penghaisoft.microwms.material.web.insidewarehouse.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.base.model.BaseDictionary;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.RawStorageDto;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.util.ExcelExpUtil;
import com.penghaisoft.microwms.material.web.insidewarehouse.service.IBaseDictionaryService;
import com.penghaisoft.microwms.material.web.insidewarehouse.service.IMaterialInsideService;

/**
 * @ClassName: InsideRawStorageController
 * @Description: 原材料库存查询
 * @author jzh
 * @date 2018年5月17日 下午1:50:02
 */
@RestController
@RequestMapping(value = "insidewarehouse/RawStorage")
@Slf4j
public class InsideRawStorageController extends BaseController {

	@Autowired
	private IMaterialInsideService iMaterialInsideService;
	@Autowired
	private IBaseDictionaryService baseDictionaryService;
//	@Autowired
//    private IUserService userService;

	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows, RawStorageDto rawStorageDto) {
		Pager<RawStorageDto> result = iMaterialInsideService.findListByCondition(page, rows, rawStorageDto);

		String sumInfo = result.getOrder();
		if (sumInfo != null && sumInfo.length() > 0) {
			result.setOrder(sumInfo);
		}

		return success(result);
	}

	/**
	 * @Title: listHZ
	 * @Description: 按照型号汇总信息
	 * @date 2019年1月28日 上午11:24:26
	 * @param page
	 * @param rows
	 * @param rawStorageDto
	 * @return TransResult
	 */
	@PostMapping("list/hz")
	public TransResult listHZ(@RequestParam(name = "page", defaultValue = "1") int page,
			@RequestParam(name = "rows", defaultValue = "10") int rows, RawStorageDto rawStorageDto) {
		Pager<RawStorageDto> result = iMaterialInsideService.findListByConditionHZ(page, rows, rawStorageDto);

		String sumInfo = result.getOrder();
		if (sumInfo != null && sumInfo.length() > 0) {
			result.setOrder(sumInfo);
		}

		return success(result);
	}

	/**
	 * @Title: getList @Description: 获取库存数据 @param @param
	 *         rawStorageDto @param @return 参数说明 @return TransResult 返回类型 @author
	 *         luoteng @date 2018年8月14日 上午11:53:29 @throws
	 */
	@PostMapping("get/list")
	public TransResult getList(RawStorageDto rawStorageDto) {
		List<RawStorageDto> result = iMaterialInsideService.queryStorageDtoList(rawStorageDto);
		return success(result);
	}

	/**
	 * @Title: listExp @Description: 原材料库存导出 @param @param
	 *         rawStorageDto @param @param request @param @param
	 *         response @param @return 参数说明 @return String 返回类型 @author jzh @throws
	 */
	@GetMapping("listExp")
	public String listExp(RawStorageDto rawStorageDto, HttpServletRequest request, HttpServletResponse response) {

		List<RawStorageDto> dataList = iMaterialInsideService.queryStorageDtoList(rawStorageDto);
		BaseDictionary baseDictionary1 = new BaseDictionary();
		baseDictionary1.setActiveFlag("1");
		baseDictionary1.setDicType("model_type");
		List<BaseDictionary> list1 = baseDictionaryService.queryAllByCondition(baseDictionary1);
		if (dataList != null && !dataList.isEmpty()) {
			for (RawStorageDto tmpData : dataList) {
				if (list1 != null && !list1.isEmpty()) {
					for (BaseDictionary tmp : list1) {
						// 判断产品类型
						if (tmp.getDicCode().equals(tmpData.getModelType())) {
							tmpData.setModelType(tmp.getDicName());
						}
					}

					// 判断货主类型
					if ("0".equals(tmpData.getUserDefined3())) {
						tmpData.setUserDefined3("本公司");
					}
					if ("1".equals(tmpData.getUserDefined3())) {
						tmpData.setUserDefined3("外公司");
					}
				}
			}
		}

		List<Map> columnList = new ArrayList<Map>();
		Map<String, String> columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "合同号");
		columnMap.put("columnBeanName", "contractNo");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "型号");
		columnMap.put("columnBeanName", "model");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "型号描述");
		columnMap.put("columnBeanName", "modelDesc");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "型号类型");
		columnMap.put("columnBeanName", "modelType");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "货主");
		columnMap.put("columnBeanName", "userDefined3");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "库存个数");
		columnMap.put("columnBeanName", "amount");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "单位");
		columnMap.put("columnBeanName", "unit");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "库存数量 ");
		columnMap.put("columnBeanName", "iventAmount");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "可用数量 ");
		columnMap.put("columnBeanName", "availAmount");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "冻结数量");
		columnMap.put("columnBeanName", "lockAmount");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "库位 ");
		columnMap.put("columnBeanName", "wlCode");
		columnList.add(columnMap);

		SXSSFWorkbook sw = ExcelExpUtil.generateWorkbook(dataList, columnList);

		OutputStream out = null;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String sdfDate = sdf.format(date);
		String fileName = "原材料库存查询" + sdfDate + ".xlsx";// 文件名
		try {
			out = response.getOutputStream();
			response.setContentType("application/force-download");// 设置强制下载不打开
			response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
			sw.write(out);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

//	//记录导出日志
//	BaseUserExportLog exportLog = new BaseUserExportLog();
//	exportLog.setTableName("OdsProKcfp");
//	String expCond = JSON.toJSONString(odsProKcfp);
//	exportLog.setExpcondition(expCond);
//	if (dataList!=null) {
//		exportLog.setExportNum(dataList.size());
//	}else {
//		exportLog.setExportNum(0);
//	}
//	exportLog.setUserDefined1("excel");
//	exportLog.setUserDefined2("库存分配查询");
//	exportLog.setFactoryCode(getCurrentFactoryCode());
//	exportLog.setCreateBy(getLoginName());
//	userService.addExportLog(exportLog);

		return "sucess";
	}

	/**
	 * @Title: listExp @Description: 原材料库存导出 @param @param
	 * rawStorageDto @param @param request @param @param response @param @return
	 * 参数说明 @return String 返回类型 @author jzh @throws
	 */
	@GetMapping("listExp/hz")
	public String listExpHz(RawStorageDto rawStorageDto, HttpServletRequest request, HttpServletResponse response) {

		List<RawStorageDto> dataList = iMaterialInsideService.queryStorageDtoListHZ(rawStorageDto);
		BaseDictionary baseDictionary1 = new BaseDictionary();
		baseDictionary1.setActiveFlag("1");
		baseDictionary1.setDicType("model_type");
		List<BaseDictionary> list1 = baseDictionaryService.queryAllByCondition(baseDictionary1);
		if (dataList != null && !dataList.isEmpty()) {
			for (RawStorageDto tmpData : dataList) {
				if (list1 != null && !list1.isEmpty()) {
					for (BaseDictionary tmp : list1) {
						// 判断产品类型
						if (tmp.getDicCode().equals(tmpData.getModelType())) {
							tmpData.setModelType(tmp.getDicName());
						}
					}

					// 判断货主类型
					if ("0".equals(tmpData.getUserDefined3())) {
						tmpData.setUserDefined3("本公司");
					}
					if ("1".equals(tmpData.getUserDefined3())) {
						tmpData.setUserDefined3("外公司");
					}
				}
			}
		}

		List<Map> columnList = new ArrayList<Map>();
		Map<String, String> columnMap = new HashMap<String, String>();
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "型号");
		columnMap.put("columnBeanName", "model");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "型号描述");
		columnMap.put("columnBeanName", "modelDesc");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "型号类型");
		columnMap.put("columnBeanName", "modelType");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "货主");
		columnMap.put("columnBeanName", "userDefined3");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "库存个数");
		columnMap.put("columnBeanName", "amount");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "单位");
		columnMap.put("columnBeanName", "unit");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "库存数量 ");
		columnMap.put("columnBeanName", "iventAmount");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "可用数量 ");
		columnMap.put("columnBeanName", "availAmount");
		columnList.add(columnMap);
		columnMap = new HashMap<String, String>();
		columnMap.put("columnComment", "冻结数量");
		columnMap.put("columnBeanName", "lockAmount");
		columnList.add(columnMap);

		SXSSFWorkbook sw = ExcelExpUtil.generateWorkbook(dataList, columnList);

		OutputStream out = null;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String sdfDate = sdf.format(date);
		String fileName = "原材料库存汇总查询" + sdfDate + ".xlsx";// 文件名
		try {
			out = response.getOutputStream();
			response.setContentType("application/force-download");// 设置强制下载不打开
			response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
			sw.write(out);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

//	//记录导出日志
//	BaseUserExportLog exportLog = new BaseUserExportLog();
//	exportLog.setTableName("OdsProKcfp");
//	String expCond = JSON.toJSONString(odsProKcfp);
//	exportLog.setExpcondition(expCond);
//	if (dataList!=null) {
//		exportLog.setExportNum(dataList.size());
//	}else {
//		exportLog.setExportNum(0);
//	}
//	exportLog.setUserDefined1("excel");
//	exportLog.setUserDefined2("库存分配查询");
//	exportLog.setFactoryCode(getCurrentFactoryCode());
//	exportLog.setCreateBy(getLoginName());
//	userService.addExportLog(exportLog);

		return "sucess";
	}

}
